package com.nidu.demo.user.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.user.convertor.UserCriteriaConvertor;
import com.nidu.demo.user.dto.UserPageQry;
import com.nidu.demo.dept.gateway.DepartmentGateway;
import com.nidu.demo.user.convertor.UserConvertor;
import com.nidu.demo.user.dto.UserCO;
import com.nidu.demo.user.gateway.UserDepartmentGateway;
import com.nidu.demo.user.gateway.UserGateway;
import com.nidu.demo.user.model.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
@RequiredArgsConstructor
public class UserPageQryExe {
    private final UserGateway gateway;

    private final UserConvertor convertor;

    private final UserCriteriaConvertor criteriaConvertor;

    private final DepartmentGateway departmentGateway;

    private final UserDepartmentGateway userDepartmentGateway;

    public PageResponse<UserCO> execute(UserPageQry qry) {
        // 1. 如果传入了deptId，需要查询该部门及其所有子部门下的用户
        if (qry.getDeptId() != null) {
            log.info("查询部门[{}]及其子部门下的用户", qry.getDeptId());
            
            // 1.1 获取部门及其所有子部门ID
            Set<Long> allDeptIds = departmentGateway.getAllChildrenIds(qry.getDeptId());

            // 1.2 获取这些部门下的所有用户ID
            Set<Long> userIds = userDepartmentGateway.getUserIdsByDeptIds(allDeptIds);

            // 1.3 将用户ID设置到查询条件中
            if (userIds.isEmpty()) {
                // 如果没有用户，返回空结果
                return PageResponse.of(qry.getPageSize(), qry.getPageIndex());
            }
            
            // 将用户ID列表设置到查询对象中（需要扩展UserPageQry）
            qry.setUserIds(userIds);
        }

        UserCriteria criteria = criteriaConvertor.toDataObject(qry);

        // 2. 执行分页查询
        PageResponse<User> page = gateway.pageByCondition(criteria);

        // 4. 转换为DTO并返回
        List<UserCO> userCOS = convertor.toClientObjects(page.getData());
        return PageResponse.of(userCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

//    public void setProfileInfos(List<User> users) {
//        // 查询角色、部门、职位信息
//        Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
//        if(!CollUtil.isEmpty(userIds)){
//            Map<Long, List<UserRole>> userRoleMap = userRoleGateway.getByUserIds(userIds);
//            Map<Long, List<UserDepartment>> userDepartmentMap = userDeptGateway.getByUserIds(userIds);
//            Map<Long, List<UserPosition>> userPositionMap = userPostGateway.getByUserIds(userIds);
//
//            users.forEach(user -> user.setProfile(userRoleMap.getOrDefault(user.getId(), new ArrayList<>()).stream().map(UserRole::getRoleId).collect(Collectors.toSet()),
//                    userDepartmentMap.getOrDefault(user.getId(), new ArrayList<>()).stream().map(UserDepartment::getDeptId).collect(Collectors.toSet()),
//                    userPositionMap.getOrDefault(user.getId(), new ArrayList<>()).stream().map(UserPosition::getPostId).collect(Collectors.toSet())
//            ));
//
//        }
//    }

}
